package com.shyroke.daydayzhuanapi.mapper;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.shyroke.daydayzhuanapi.dto.AppDto;
import com.shyroke.daydayzhuanapi.dto.LabelDto;
import com.shyroke.daydayzhuanapi.dto.TopAppDto;
import com.shyroke.daydayzhuanapi.entity.AppEntity;
import com.shyroke.daydayzhuanapi.entity.LabelEntity;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class AppMapper {
    @Autowired
    private ModelMapper modelMapper;

    public AppDto entityToDto(AppEntity e) {
        AppDto dto = modelMapper.map(e, AppDto.class);
        dto.setCreateTime2(DateUtil.format(e.getCreateTime(),"yyyy-MM-dd"));
        List<LabelEntity> labelList = e.getLabelEntityList();
        if(CollectionUtil.isNotEmpty(labelList)){
            List<LabelDto> labelDtos = labelList.stream().map(label -> modelMapper.map(label, LabelDto.class)).collect(Collectors.toList());
            dto.setLabelList(labelDtos);
            if(CollectionUtil.isNotEmpty(labelDtos)){
                dto.setFirstLableName(labelDtos.get(0).getLabelName());
            }
        }
        Integer device = dto.getDevice();
        if(device != null){
            switch (device){
                case 0:
                    dto.setShowDevice("安卓/苹果");
                    break;
                case 1:
                    dto.setShowDevice("安卓");
                    break;
                case 2:
                    dto.setShowDevice("苹果");
                case 3:
                    dto.setShowDevice("电脑客户端");
                    break;
                default:
                    break;
            }
        }
        return dto;
    }

    public AppEntity dtoToEntity(AppDto app) {
        return modelMapper.map(app,AppEntity.class);
    }

    public AppEntity dtoToUpdateEntity(AppDto app, AppEntity appEntity) {
        appEntity.setCompanyName(app.getCompanyName());
        appEntity.setContent(app.getContent());
        appEntity.setCover(app.getCover());
        appEntity.setDevice(app.getDevice());
        appEntity.setDownCount(app.getDownCount());
        appEntity.setFalseDownCount(app.getFalseDownCount());
        appEntity.setInviteImg(app.getInviteImg());
        appEntity.setInviteUrl(app.getInviteUrl());
        appEntity.setKeyword(app.getKeyword());
        appEntity.setMark(app.getMark());
        appEntity.setTitle(app.getTitle());
        appEntity.setWithdraw(app.getWithdraw());
        return appEntity;
    }

    public TopAppDto dtoToAppDto(AppDto appDto) {
        TopAppDto dto = modelMapper.map(appDto, TopAppDto.class);
        dto.setDetailUrl("https://www.daydayzhuan.com/app/"+ appDto.getId()+"?channel=APP");
        return dto;
    }
}
